﻿using System.Collections.Generic;
using System.Linq;
using Game.Map;
using Game.Plugin;
using Game.Plugin.Parse;

namespace DebugPlugin
{
    public class FieldsPlugin : AbstractPlugin
    {
        readonly IGameMapView _GameMapView;

        public FieldsPlugin(
            IGamePlugin parent,
            IGameMapView view)
            : base(parent, "fields")
        {
            _GameMapView = view;
        }

        public override ICodeObject Parse(
            string token, 
            ParserContext context)
        {
            IExpression e = new FieldsExpression(_GameMapView);
            context.Stack.Push(e);
            return e;
        }
    }

    class FieldsExpression : IExpression
    {
        readonly IGameMapView _GameMapView;

        public FieldsExpression(IGameMapView view)
        {
            _GameMapView = view;
        }

        public object Interpret(Dictionary<string, ICodeObject> variables)
        {
            string s = _GameMapView.Fields.Aggregate("", (current, e) => current + ("(" + e.Point + ")"));
            return s;
        }
    }
}
